package dao.mededelingen;

import java.sql.ResultSet;
import java.sql.SQLException;

import dao.DataConnection;
import mededelingen.model.MopVanDeDag;
import mededelingen.model.MopVanDeDagLijst;

public class DataLayerMVDD extends DataConnection {

	private MopVanDeDag mvdd= new MopVanDeDag("","");
	private  String mop;
	String date;
	private ResultSet rs;
	String sql;

	public void insertMVDD(MopVanDeDag m){
		openConn();	
		String datum= m.getJaar()+"-"+m.getMaand()+"-"+m.getDag();
		sql = "INSERT INTO "
				+"`projectm`.`mop`("
				+"`mop`,"
				+"`datum"+"`)"
				+"VALUES('"+m.getMop()+"','"+datum+"');";

		try {
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//System.out.println(sql);
	}

	public  MopVanDeDag select(int id){
		openConn();	
		sql="select * from mop where mopId ='"+id+"'" ;
		try {
			stmt = conn.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
			
				mvdd.setMop(rs.getString("mop"));
			    mvdd.SetDatum(rs.getString("datum"));
			}
			
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mvdd;
	}


	public void delete(int id){
		openConn();	
		sql="DELETE FROM `projectm`.`mop` WHERE `mopID`='"+id+"';";

		try {
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(sql);
	}
	
	//methode om the mop uit de database laden
	public void loadMop(){
		String sql = "Select * FROM mop";
		try{
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql); // hij pakt de query String sql
			MopVanDeDag mvdd;
			
			while (rs.next()){
				mvdd = new MopVanDeDag();
				mvdd.setMop(rs.getString("mop"));
				mvdd.SetDatum(rs.getString("datum"));
				MopVanDeDagLijst.getInstance().addMop(mvdd);
			}
			
		}
		catch(SQLException sqle){
			sqle.printStackTrace();
		}
	}
}
